package main;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

import equipment.*;
import unit.*;

import java.util.ArrayList;
import java.util.Random;

public class Game { 
	
	public static Board board;
	public static Army[] armies = {null, null};
	public static ArrayList<Battle> battles = new ArrayList<Battle>();
	public static final int GOODINDEX = 0;
	public static final int BADINDEX = 1;
	
	public Unit selectedUnit = null;
	public Unit shootTarget = null;
	
	public int priority = 0;
	public int currArmy = 0;
	public String currPhase = "Move";
	
	public static Random rng = new Random();
	
	public static UI myUI;
	
	public Game() {
		Board b = new Board(24, 24);
		this.board = b;
		
		Unit h1 = new HighElf(1,0,50,50);
		h1.equip(new ElfBow());
		h1.equip(new ElvenBlade());
		h1.equip(new Shield());
		Unit h2 = new HighElf(2,0,100,50);
		h2.equip(new ElfBow());
		
		Unit g1 = new Goblin(11,1,50,150);
		g1.equip(new OrcBow());
		Unit g2 = new Goblin(12,1,100,150);
		g2.equip(new Shield());
		Unit g3 = new Goblin(13,1,150,150);
		g3.equip(new Shield());
		g3.equip(new Spear());
		
		ArrayList<Unit> a1 = new ArrayList<Unit>();
		a1.add(h1);
		a1.add(h2);
		Army goodArmy = new Army(a1);
		
		ArrayList<Unit> a2 = new ArrayList<Unit>();
		a2.add(g1);
		a2.add(g2);
		a2.add(g3);
		Army badArmy = new Army(a2);
		
		armies[0] = goodArmy;
		armies[1] = badArmy;
		battles = new ArrayList<Battle>();
		selectedUnit = null;
		priority = 0;
		currArmy = 0;
		currPhase = "Move";
	}
	
	public void setupTest() {
		
	}
	
	public void endPhase() {
		this.selectedUnit = null;
		this.shootTarget = null;
		myUI.message = "";
		if (currPhase == "Fight") {
			// Resolve fights (this needs to change)!!!!!!!!!!!
			if (battles.size() > 0) {
				myUI.message = "Still battles to fight";
				myUI.repaint();
				return;
			}
			
			int winner = this.winConditions();
			if (winner == 0) {
				myUI.message = "Good side wins!";
				myUI.repaint();
				return;
			}
			else if (winner == 1) {
				myUI.message = "Bad side wins!";
				myUI.repaint();
				return;
			}
			else if (winner == 2) {
				myUI.message = "It's a draw!";
				myUI.repaint();
				return;
			}
			
			// Calculate priority
			int priority0 = rng.nextInt(6);
			int priority1 = rng.nextInt(6);
			if (priority0 > priority1)
				priority = 0;
			else if (priority1 > priority0)
				priority = 1;
			else
				priority = (priority+1)%2;
			
			// Refresh moves
			for (Army a : armies) {
				for (Unit u : a.units) {
					u.refresh();
				}
			}
			currArmy = priority;
			
			currPhase = "Move";
			myUI.closeFightUI();
			myUI.openMoveUI();
			myUI.removeMouseListener(myUI.fightMouse);
			myUI.removeMouseMotionListener(myUI.fightMouse);
			myUI.addMouseListener(myUI.moveMouse);
			myUI.addMouseMotionListener(myUI.moveMouse);
		}
		else if (priority == currArmy) {
			currArmy = (currArmy+1)%2;
		}
		else {
			currArmy = priority;
			if (currPhase == "Move") {
				currPhase = "Shoot";
				myUI.closeMoveUI();
				myUI.openShootUI();
				myUI.removeMouseListener(myUI.moveMouse);
				myUI.removeMouseMotionListener(myUI.moveMouse);
				myUI.addMouseListener(myUI.shootMouse);
				myUI.addMouseMotionListener(myUI.shootMouse);
			}
			else if (currPhase == "Shoot") {
				for (Army a : Game.armies) {
					for (Unit u : a.units){
						u.moveLeft = 0;
					}
				}
				currPhase = "Fight";
				myUI.closeShootUI();
				myUI.openFightUI();
				myUI.removeMouseListener(myUI.shootMouse);
				myUI.removeMouseMotionListener(myUI.shootMouse);
				myUI.addMouseListener(myUI.fightMouse);
				myUI.addMouseMotionListener(myUI.fightMouse);
			}
		}
		myUI.repaint();
	}

	public int winConditions() {
		if (armies[0].units.size() < armies[0].origSize/4) {
			if (armies[1].units.size() < armies[1].origSize/4)
				return 2;
			else
				return 1;
		}
		if (armies[1].units.size() < armies[1].origSize/4)
			return 0;
		return -1;
	}
}